package libcore.view

import android.content.Context
import android.content.res.Resources
import android.util.AttributeSet
import android.view.View

/**
 * 通知栏高度的view，用于浸没式
 */
class ImmersiveView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
    companion object {
        var sStatusBarHeight = -1
    }

    constructor(context: Context) : this(context, null)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(widthMeasureSpec, statusBarHeight)
    }

    private val statusBarHeight: Int
        get() {
            if (sStatusBarHeight != -1) {
                return sStatusBarHeight
            }
            sStatusBarHeight = try {
                Resources.getSystem()
                    .getDimensionPixelSize(
                        Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android")
                    )
            } catch (e: Throwable) {
                25
            }
            if (sStatusBarHeight <= 0) {
                sStatusBarHeight = 25
            }
            return sStatusBarHeight
        }
}